#include "stdafx.h"
#include "net/URIStreamOpener.hpp"
#include "net/URIStreamFactory.hpp"
#include "net/URI.hpp"
#include "stdlib/exception.hpp"
using namespace JHCPP::stdlib::jhException;
#include "designpattern/singletonholder.hpp"
using JHCPP::designpattern::TSingletonHolder;

NET_NAMESPACE_BEGIN

CURIStreamOpener::CURIStreamOpener()
{
	registerStreamFactory("file", new CFileStreamFactory);
}

CURIStreamOpener::~CURIStreamOpener()
{
	for (FactoryMap::iterator it = m_map.begin(); it != m_map.end(); ++it)
		delete it->second;
}

std::istream* CURIStreamOpener::open(const CURI& uri) const
{
	CMutex::ScopedLock lock(m_mutex);

	std::string scheme;
	if (uri.isRelative())
		scheme = "file";
	else
		scheme = uri.getScheme();
	return openURI(scheme, uri);
}

std::istream* CURIStreamOpener::open(const std::string& pathOrURI) const
{
	CMutex::ScopedLock lock(m_mutex);

	try
	{
		CURI uri(pathOrURI);
		std::string scheme(uri.getScheme());
		FactoryMap::const_iterator it = m_map.find(scheme);
		if (it != m_map.end())
			return openURI(scheme, uri);
	}
	catch (CException&)
	{
	}
	CPath path(pathOrURI, CPath::PATH_WINDOWS);
	return openFile(path);
}

std::istream* CURIStreamOpener::open(const std::string& basePathOrURI, const std::string& pathOrURI) const
{
	CMutex::ScopedLock lock(m_mutex);

	try
	{
		CURI uri(basePathOrURI);
		std::string scheme(uri.getScheme());
		FactoryMap::const_iterator it = m_map.find(scheme);
		if (it != m_map.end())
		{
			uri.resolve(pathOrURI);
			return openURI(scheme, uri);
		}
	}
	catch (CException&)
	{
	}
	CPath base(basePathOrURI, CPath::PATH_WINDOWS);
	CPath path(pathOrURI, CPath::PATH_WINDOWS);
	base.resolve(path);
	return openFile(base);
}

void CURIStreamOpener::registerStreamFactory(const std::string& scheme, CURIStreamFactory* pFactory)
{
	jh_check_ptr (pFactory);

	CMutex::ScopedLock lock(m_mutex);
	if (m_map.find(scheme) == m_map.end())
	{
		m_map[scheme] = pFactory;
	}
	else throw ExistsException("An URIStreamFactory for the given scheme has already been registered", scheme);
}

void CURIStreamOpener::unregisterStreamFactory(const std::string& scheme)
{
	CMutex::ScopedLock lock(m_mutex);

	FactoryMap::iterator it = m_map.find(scheme);
	if (it != m_map.end())
	{
		CURIStreamFactory* pFactory = it->second;
		m_map.erase(it);
		delete pFactory;
	}
	else throw NotFoundException("No URIStreamFactory has been registered for the given scheme", scheme);
}

bool CURIStreamOpener::supportsScheme(const std::string& scheme)
{
	CMutex::ScopedLock lock(m_mutex);
	return m_map.find(scheme) != m_map.end();
}

static TSingletonHolder<CURIStreamOpener> sh;

CURIStreamOpener& CURIStreamOpener::defaultOpener()
{
	return *sh.get();
}

std::istream* CURIStreamOpener::openFile(const CPath& path) const
{
	CFileStreamFactory factory;
	return factory.open(path);
}

std::istream* CURIStreamOpener::openURI(const std::string& scheme, const CURI& uri) const
{
	std::string actualScheme(scheme);
	CURI actualURI(uri);
	int redirects = 0;

	while (redirects < MAX_REDIRECTS)
	{
		try
		{
			FactoryMap::const_iterator it = m_map.find(actualScheme);
			if (it != m_map.end())
				return it->second->open(actualURI);
			else if (redirects > 0)
				throw UnknownURISchemeException(actualURI.toString() + std::string("; redirected from ") + uri.toString());
			else
				throw UnknownURISchemeException(actualURI.toString());
		}
		catch (CURIRedirection& redir)
		{
			actualURI = redir.uri();
			actualScheme = actualURI.getScheme();
			++redirects;
		}
	}
	throw IOException("Too many redirects while opening URI", uri.toString());
}

NET_NAMESPACE_END
